﻿using Digitalmes.Core.Domain.Eitites;
using Digitalmes.Core.Domain.Repositories;

namespace Digitalmes.Core.Services.Impl;

internal sealed class TurntableArchiveService(IRepository<TurntableArchiveRecord> repo) : ITurntableArchiveService
{
    public async Task<(List<TurntableArchiveRecord>, int)> GetPageListAsync(DateTime? startTime, DateTime? endTime, int pageIndex, int pageSize, CancellationToken cancellationToken = default)
    {
        return await repo.AsQueryable()
            .WhereIF(startTime.HasValue, m => m.ArchiveTime >= startTime.MinTime())
            .WhereIF(endTime.HasValue, m => m.ArchiveTime <= endTime.MaxTime())
            .ToPagationAsync(pageIndex, pageSize, cancellationToken);
    }

    public async Task<List<TurntableArchiveRecord>> GetListAsync(DateTime? startTime, DateTime? endTime, CancellationToken cancellationToken = default)
    {
        return await repo.AsQueryable()
            .WhereIF(startTime.HasValue, m => m.ArchiveTime >= startTime.MinTime())
            .WhereIF(endTime.HasValue, m => m.ArchiveTime <= endTime.MaxTime())
            .ToListAsync(cancellationToken);
    }

    public async Task ArchiveAsync(TurntableArchiveRecord input)
    {
        await repo.InsertAsync(input);
    }
}
